﻿using Newtonsoft.Json.Linq;
using SharpSoft.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace AccountModule
{
    public class AccountDAL
    {
        private readonly Database db;
        public AccountDAL(Database p_db)
        {
            db = p_db;
        }

        public JObject CreateAccount(JObject account)
        {
            string sql = @"INSERT INTO Sys_Accounts(AccountName,Password)VALUES(@AccountName,@Password);
Select AccountId,AccountName FROM Sys_Accounts WHERE AccountId=LASTID();
";
            return db.ExecuteJObject(sql);
        }

        public void UpdateStatus(int accountId, int status)
        {

            string sql = $@"UPDATE Sys_Accounts SET `Status`={status} WHERE AccountId={accountId};
";
            db.ExecuteNonQuery(sql);
        }

        public bool CheckPassword(int accountId, string password)
        {
            string sql = $"SELECT COUNT(*) FROM Sys_Accounts WHERE AccountId={accountId} AND Password='{SharpSoft.Security.MD5Hash(password)}'";
            return db.ExecuteInt32(sql) == 1;
        }

        public int? GetAccountId(string accountName)
        {
            string sql = $"SELECT AccountId FROM Sys_Accounts WHERE AccountName=@AccountName";
            var id = db.ExecuteScalar<int?>(sql, new { AccountName = accountName });
            return id;
        }

    }
}
